package pacBioSimple;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;

import pacBiopackage.GetOpt;

public class PacBioLinkerScreenMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GetOpt go = new GetOpt(args, "o:i:k:s:h");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		String filenameInput = "";   
		String filenameOutput="output.txt";
		int kmerSize=17;
		int sw_window=50;
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		    else if ((char)ch == 'i') {
		    	filenameInput = go.optArgGet();
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='k')
		    {
		    	kmerSize = Integer.valueOf(go.optArgGet());
		    }
		    else if((char)ch =='s')
		    {
		    	sw_window = Integer.valueOf(go.optArgGet());
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameInput.isEmpty()) {
		    System.out.println("Usage: -i PacBio [ -o outputFile -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-i " + filenameInput); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N	
		System.out.println("-k "+ kmerSize);
		System.out.println("-s "+ sw_window);
		System.out.println("------------------------");
		
		PrintWriter pw=null;
		PrintWriter pw2=null;
		PrintWriter pw3=null;
		
		BufferedReader br = null;
		try
		{
			
			Writer fw = new FileWriter(filenameOutput);
			Writer bw = new BufferedWriter(fw);
			pw2 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_linker")));
			pw3 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_string_value_r")));
			pw = new PrintWriter(bw);
			br = new BufferedReader(new FileReader(filenameInput));
			
			//PacBioCorrectionByKmers kmerCorrection = new PacBioCorrectionByKmers(ks,stat,freq_mode,freq_table);
			
			System.out.println("starting correction");
			PacBioLinkerScreenKmer screen_it = new PacBioLinkerScreenKmer();
			while(br.ready())
			{
				//test if we are getting a copy of br or if its counting up
				PacBioReadContainer read = new PacBioReadIntoContainer().readIntoContainer(br);
				//System.out.println("read "+ read.getHeader() );
				ArrayList<Double>kmerD = new ArrayList<Double>();
				if(screen_it.screen_seq(read.getOrginalSequence(), kmerSize, 0.5,kmerD))
				{
					//System.out.println("found");
					//kmerD=(ArrayList<Double>)screen_it.slinding_window(sw_window, kmerD);
					kmerD=(ArrayList<Double>)screen_it.conntect_ones(20, kmerD);
					kmerD=(ArrayList<Double>)screen_it.checkBlockWidth(20, kmerD);
					screen_it.cut_and_align(kmerD, read);
					
				//	kmerD=(ArrayList<Double>)screen_it.conntect_ones(20, kmerD);
				//	kmerD=(ArrayList<Double>)screen_it.checkBlockWidth(10, kmerD);
					StringBuilder sb = new StringBuilder();
					for(int i=0; i < kmerD.size();++i)
					{
						sb.append(kmerD.get(i));
						if(i+1<kmerD.size())
						{
							sb.append("\t");
						}
					}
					pw3.println(sb.toString());
					pw2.println(read.getHeader());
					pw2.println(read.getOrginalSequence());	
				}
				else
				{
					//System.out.println(" not found ");
					pw.println(read.getHeader());
					pw.println(read.getOrginalSequence());
				}	
			}
			pw.flush();
			pw.close();
			pw2.flush();
			pw2.close();
			pw3.flush();
			pw3.close();
			screen_it.write_stat(filenameOutput+"_linker_stat");
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}

	}


}
